package com.example.walled_houses.dto;
import com.example.walled_houses.entity.House;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

import static com.example.walled_houses.utils.TengXunConstants.TENGXUN_PATH;


@Data
public class HouseDTO {

    private Integer houseId;

    /**
     * 围屋的名称
     */
    private String houseName;

    /**
     * 围屋的地址
     */
    private String houseAddress;

    /**
     * 围屋评分
     */
    private Double houseScore;

    /**
     * 围屋所在的城市
     */
    private String houseCity;

    /**
     * 围屋星级
     */
    private Integer houseStar;

    /**
     *  围屋的经纬度
     */
    private String location;

    /**
     * 围屋图片地址
     */
    private List<String> housePicture;

    /**
     * 围屋描述
     */
    private String houseDescription;


    /**
     * 有参构造函数
     * @param house
     */
    public HouseDTO(House house){
        this.houseId = house.getHouseId();
        this.houseName = house.getHouseName();
        this.houseAddress = house.getHouseAddress();
        this.houseScore = house.getHouseScore();
        this.houseCity = house.getHouseCity();
        this.houseStar = house.getHouseStar();
        this.location = house.getHouseLatitude() + "," + house.getHouseLongitude();
        if(house.getHousePicture() != null){
            Gson gson = new Gson();
            List<String> pictureList = gson.fromJson(house.getHousePicture(), new TypeToken<List<String>>() {
            }.getType());
            List<String> photoList = new ArrayList<>(pictureList.size());
            pictureList.forEach(picture->{
                photoList.add(TENGXUN_PATH + picture);
            });
            this.housePicture = photoList;
        }

        this.houseDescription = house.getHouseDescription();
    }

}
